package com.ruoyi.flowable.controller;

import java.util.List;

import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.RemoteUser;
import org.flowable.ui.common.model.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;

@RestController
public class FlowableIndexController extends BaseController {

    private final ISysUserService sysUserService;

    private final ISysPostService sysPostService;

    private final IdentityService identityService;

    public FlowableIndexController(ISysUserService sysUserService, ISysPostService sysPostService, IdentityService identityService) {
        this.sysUserService = sysUserService;
        this.sysPostService = sysPostService;
        this.identityService = identityService;
    }

    @GetMapping(value = "/rest/account", produces = "application/json")
    public UserRepresentation getAccount() {
        SysUser sysUser = ShiroUtils.getSysUser();
        RemoteUser remoteUser = new RemoteUser();
        remoteUser.setFirstName(sysUser.getUserName());
        remoteUser.setFullName(sysUser.getLoginName());
        remoteUser.setEmail(sysUser.getEmail());
        remoteUser.setId(sysUser.getUserId().toString());
        //构建用户代表类
        UserRepresentation userRepresentation = new UserRepresentation(remoteUser);
        return userRepresentation;
    }

    @PostMapping(value = "/flowable/userAndGroup/sync", produces = "application/json")
    public AjaxResult userAndGroupSync(SysUser sysUser, SysPost sysPost) {
        try {
            List<SysUser> userList=sysUserService.selectUserList(sysUser);
            List<SysPost> postList=sysPostService.selectPostList(sysPost);
            // 同步用户关系
            for (SysUser user: userList){
                User flowableUser=identityService.createUserQuery().userId(user.getLoginName()).singleResult();
                if (flowableUser==null){
                    flowableUser=identityService.newUser(user.getLoginName());
                    flowableUser.setDisplayName(sysUser.getUserName());
                    flowableUser.setEmail(sysUser.getEmail());
                    //PWD_ 存放 用户电话号
                    flowableUser.setPassword(sysUser.getPhonenumber());
                    //FIRST_ 存放 用户ID
                    flowableUser.setFirstName(sysUser.getUserId().toString());
                    //LAST_存放部门ID
                    flowableUser.setLastName(sysUser.getDeptId().toString());
                    identityService.saveUser(flowableUser);
                }
            }
            // 同步组关系
            for (SysPost post: postList){
                Group flowableGroup = identityService.createGroupQuery().groupId(post.getPostCode()).singleResult();
                if (flowableGroup == null) {
                    flowableGroup = identityService.newGroup(sysPost.getPostCode());
                }
                flowableGroup.setName(sysPost.getPostName());
                flowableGroup.setType(sysPost.getPostId().toString());
                identityService.saveGroup(flowableGroup);
            }
            // 同步用户与组的关系
            for (SysUser user: userList){
              Long[] postIds=user.getPostIds();
                for (Long postId : postIds) {
                    SysPost post=sysPostService.selectPostById(postId);
                    if (post!=null){
                        identityService.createMembership(user.getLoginName(),post.getPostCode());
                    }
                }
            }
            return success("同步成功！");
        } catch (Exception e) {
            return error(e.getMessage());
        }

    }

}
